package io.geekidea.boot.shop.service.base.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.geekidea.boot.framework.exception.BusinessException;
import io.geekidea.boot.framework.page.OrderByItem;
import io.geekidea.boot.framework.page.OrderMapping;
import io.geekidea.boot.framework.page.Paging;
import io.geekidea.boot.shop.dto.SignRewardRecordDto;
import io.geekidea.boot.shop.entity.SignRewardRecord;
import io.geekidea.boot.shop.mapper.SignRewardRecordMapper;
import io.geekidea.boot.shop.query.app.AppSignRewardRecordQuery;
import io.geekidea.boot.shop.query.web.SignRewardRecordQuery;
import io.geekidea.boot.shop.service.base.api.SignRewardRecordService;
import io.geekidea.boot.shop.vo.app.AppSignRewardRecordVo;
import io.geekidea.boot.shop.vo.web.SignRewardRecordVo;
import io.geekidea.boot.util.PagingUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *  服务实现类
 *
 * @author geekidea
 * @since 2024-10-27
 */
@Slf4j
@Service
public class SignRewardRecordServiceImpl extends ServiceImpl<SignRewardRecordMapper, SignRewardRecord> implements SignRewardRecordService {

    @Autowired
    private SignRewardRecordMapper signRewardRecordMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addSignRewardRecord(SignRewardRecordDto dto) {
        SignRewardRecord signRewardRecord = new SignRewardRecord();
        BeanUtils.copyProperties(dto, signRewardRecord);
        return save(signRewardRecord);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateSignRewardRecord(SignRewardRecordDto dto) {
        Integer id = dto.getId();
        SignRewardRecord signRewardRecord = getById(id);
        if (signRewardRecord == null) {
            throw new BusinessException("不存在");
        }
        BeanUtils.copyProperties(dto, signRewardRecord);
        return updateById(signRewardRecord);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteSignRewardRecord(Integer id) {
        return removeById(id);
    }

    @Override
    public SignRewardRecordVo getSignRewardRecordById(Integer id) {
        return signRewardRecordMapper.getSignRewardRecordById(id);
    }

    @Override
    public Paging<SignRewardRecordVo> getSignRewardRecordPage(SignRewardRecordQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<SignRewardRecordVo> list = signRewardRecordMapper.getSignRewardRecordPage(query);
        Paging<SignRewardRecordVo> paging = new Paging<>(list);
        return paging;
    }

    @Override
    public AppSignRewardRecordVo getAppSignRewardRecordById(Integer id) {
        return signRewardRecordMapper.getAppSignRewardRecordById(id);
    }

    @Override
    public Paging<AppSignRewardRecordVo> getAppSignRewardRecordPage(AppSignRewardRecordQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<AppSignRewardRecordVo> list = signRewardRecordMapper.getAppSignRewardRecordPage(query);
        Paging<AppSignRewardRecordVo> paging = new Paging<>(list);
        return paging;
    }

}
